package com.suanfa.collect;

import com.alibaba.fastjson.JSON;

import java.util.ArrayList;
import java.util.List;

/**
 * 两个二维数组的交集
 */
public class MixedCollect {

    public static void main(String[] args) {
        int[][] a = {{0,2},{5,10},{13,23},{24,25}};
        int[][] b = {{1,5},{8,12},{15,24},{25,26}};
        System.out.println(JSON.toJSONString(getMixed(a, b)));
    }


    public static int[][] getMixed(int[][] a, int[][]b){
        List<int[]> result = new ArrayList<>();

        int i = 0, j=0;

        while(i< a.length && j < b.length){
            int low = Math.max(a[i][0], b[j][0]);
            int hight = Math.min(a[i][1], b[j][1]);

            if(low <=hight){
                result.add(new int[]{low, hight});
            }
            if(a[i][1] <=b[j][1]){
                i++;
            }else {
                j++;
            }
        }
        return result.toArray(new int[result.size()][]);

    }
}
